package com.leetcode.algorithm.y22.m07.w4;

import java.util.HashMap;
import java.util.Map;

/**
 * 290. 单词规律
 * 
 * https://leetcode.cn/problems/word-pattern/
 * 
 * @author jie.deng
 *
 */
class Question0290Solution01 {
	
	public boolean wordPattern(String pattern, String s) {
		Map<Character, String> ch2Str = new HashMap<>();
		Map<String, Character> str2ch = new HashMap<>();
		String[] arr = s.split(" ");
		if (pattern.length() != arr.length) {
			return false;
		}
		for (int i = 0; i < arr.length; i++) {
			char ch = pattern.charAt(i);
			if (ch2Str.containsKey(ch)) {
				if (ch2Str.get(ch).equals(arr[i])) {
					continue;
				}
				return false;
			}
			if (str2ch.containsKey(arr[i])) {
				return false;
			}
			ch2Str.put(ch, arr[i]);
			str2ch.put(arr[i], ch);
		}
		return true;
	}
    
}